﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using WebBrowserTest.AnalyzeResults;

namespace WebBrowserTest.Analyzers
{
    class BackLinksAnalyzer : AbstractAnalyzer
    {
         
        public string LookupExpression
        {
            get { return _lookupExpr; }
            set { _lookupExpr = value; }
        }

        public BackLinksAnalyzer()
        {
            _lookupExpr = "<span class=\"btn\">Inlinks \\(([^\\)]+)\\)|<h2>(No inlinks found)</h2>|(error 999)";
        }
        public BackLinksAnalyzer(string lookupExpr)
        {
            this.LookupExpression = lookupExpr;
        }

        public override AbstractAnalyzeResult GetAnalyseResult(string html, string url)
        {
            var matches = Regex.Matches(html, LookupExpression);
            var backlinkCounter = 0;
            for (var i = 0; i < matches.Count; i++)
            {
                var backLinksVal = matches[i].Groups[1].Value.Replace(",","");
                if(int.TryParse(backLinksVal,out backlinkCounter))
                {
                    break;
                }
            }
            return new DefaultResult {Value = backlinkCounter,DomainName = url, Date = DateTime.Now};
        }
    }
}
